<template>
	<el-dialog title="新建课程" :visible.sync="visible" width="768px" top="0" append-to-body>
		<el-form v-bind="formConfig" :model="formData" :rules="formRules" label-width="80px">
			<el-row type="flex" :gutter="24">
				<el-col>
					<el-form-item label="课程名称" prop="title">
						<el-input v-model="formData.title" placeholder="请输入课程名称"></el-input>
					</el-form-item>
					<el-form-item label="课程编码" prop="code">
						<el-input v-model="formData.code" placeholder="请输入课程编码"></el-input>
					</el-form-item>
					<el-form-item label="课程来源" prop="extFiled2">
						<el-input v-model="formData.extFiled2" placeholder="请输入课程来源"></el-input>
					</el-form-item>
					<el-form-item label="资源分类" prop="labelValue">
						<el-select v-model="formData.labelValue" placeholder="请选择资源分类" multiple>
							<el-option v-for="(item, index) in courseLabel" :key="index" :label="item.name" :value="item.id" />
						</el-select>
					</el-form-item>
					<el-form-item label="学习时长" prop="sumHours">
						<el-input-number v-model="formData.sumHours" controls-position="right" :min="1"></el-input-number>
						<span class="unit">小时</span>
					</el-form-item>
				</el-col>
				<el-col style="width: 272px">
					<el-form-item prop="fileId" label-width="0">
						<sw-image-uploader v-model="formData.fileId" clip ratio="16:9" size="240"></sw-image-uploader>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="课程简介">
				<el-input v-model="formData.content" type="textarea" :autosize="{ minRows: 6 }" placeholder="请输入课程简介" />
			</el-form-item>
		</el-form>
		<template slot="footer">
			<el-button size="medium" @click="visible = false">取消</el-button>
			<el-button size="medium" type="primary" @click="handleSubmitForm()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script>
import { mapGetters } from 'vuex';
import formMixins from '@/mixins/formMixins';
import { addedCourse, updateCourse } from '@/api/course/sysCourse';
export default {
	mixins: [formMixins],
	data() {
		return {
			formRules: {
				title: [{ required: true, message: '请输入课程名称', trigger: 'blur' }],
				extFiled2: [{ required: true, message: '请输入课程来源', trigger: 'blur' }],
				labelValue: [{ required: true, message: '请选择资源分类', trigger: 'change' }],
				sumHours: [{ required: true, message: '请输入学习时长', trigger: 'blur' }]
			}
		};
	},
	computed: {
		...mapGetters(['userInfo', 'courseLabel']),
		callFunction() {
			return eval(this.addOrEdit ? addedCourse : updateCourse);
		},
		creatorData() {
			let userId = this.userInfo.id;
			return { auditStatus: 2, userId, orderBy: 1 };
		}
	},
	methods: {
		async beforeSubmitForm() {
			let labelValue = this.formData?.labelValue?.join(',') || '';
			this.$set(this.formData, 'status', '2');
			this.$set(this.formData, 'labelType', '6');
			this.$set(this.formData, 'sourceType', '2');
			this.$set(this.formData, 'courseType', '6');
			this.$set(this.formData, 'auditStatus', '2');
			this.$set(this.formData, 'labelValue', labelValue);
			this.$set(this.formData, 'sourceUnit', this.userInfo.deptId);
			this.$set(this.formData, 'sysCourseTeamList', [{ ...this.creatorData }]);
			return Promise.resolve(this.formData);
		}
	}
};
</script>

<style lang="scss" scoped>
.added_course_form {
	flex: 1;
	padding: 10px 6px 0;
	.cover {
		float: right;
	}
	.attrs {
		margin-right: 270px;
		overflow: hidden;
	}
}
</style>
